var navTab = [
	{id:'index', url:'include/index.html', title:null},
	{id:'product', url:'include/product.html', title:'产品管理'},
	{id:'message', url:'include/message.html', title:'消息中心'},
	{id:"center", url:'include/center.html', title:null}
];
var webviewStyles = {
	top:45 + bexta.getStatusHeight() + 'px',
	bottom:"55px"
},aniShow = {}, getNavWiteId = function(oid){
	var id;
	mui.each( navTab, function(){
		if( this.id === oid ){
			id = this;
		}
	});
	return id;
};

mui.plusReady(function () {
	var self = bexta.getWebview();
   	for( var index = 0, len = navTab.length; index < len; index++ ){
   		var temp = {};
   		if( index > 0 ){
   			var subPage;
   			if( !navTab[index].title ){
   				subPage = plus.webview.create(navTab[index].url, navTab[index].id, {top:0, bottom:webviewStyles.bottom});
   			}else{
   				subPage = plus.webview.create(navTab[index].url, navTab[index].id, webviewStyles);
   			}
   			subPage.hide();
   			self.append(subPage);
   		}else{
			//首页, 双首页模式   			
   			temp[navTab[index]] = true;
   			mui.extend(aniShow, temp);
   		}
   	}
});

var activeTab = navTab[0].id;
var header = document.querySelector('header');
header.style.paddingTop = bexta.getStatusHeight() + 'px';
header.style.height = (44 + bexta.getStatusHeight()) + 'px';
mui('.mui-bar-tab').on('tap', 'a', function() {
	if( tid != 'index' ){
		var merchant = bexta.getStorage(MERCHANT);
		if(  !merchant ){
			mui.openWindow({
				id:'login',
				url:'include/login.html',
				show:{autoShow:true,aniShow:'zoom-fade-out'}
			});
		}
	}
	var tid = this.id;
	if( activeTab === tid ) return ;
	var nav = getNavWiteId(tid);
	//header 判断
	if( !nav.title ) header.style.display = 'none';
	else header.style.display = 'block';
	
	//显示相应页面
	if( mui.os.ios || aniShow[tid] ){
		plus.webview.show(tid);
		if( !aniShow[tid] ){
			var temp = {};
			temp[tid] = true;
			mui.extend(aniShow, temp);
			mui.fire(plus.webview.getWebviewById(tid), 'VueShow');
		}
	}else{
		var temp = {};
		temp[tid] = true;
		mui.extend(aniShow, temp);
		plus.webview.show(tid, 'fade-in', 300);
		mui.fire(plus.webview.getWebviewById(tid), 'VueShow');
	}
	plus.webview.hide(activeTab);
	activeTab = tid;
});


//模拟点击事件
window.addEventListener('toggleClick', function(evt){
	var path = evt.detail.path;
	var twos = evt.detail.twos;
	var tabid = document.getElementById(path);
	mui.trigger(tabid, 'tap');
	var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active");
	if (tabid !== current) {
		current.classList.remove('mui-active');
		tabid.classList.add('mui-active');
	}
	if( twos && twos.id !='' ){
		//二级页面
		//传第参到到二级页面，操作 
		mui.fire(bexta.getWebview(path[0]), 'openPath', {
			path:twos
		});
	}
});
